﻿using UnityEngine;
using Chat.Common;

public class ChatMsgHandle : IHandleable
{
    public void Handle(string clientId,string cmd)
    {
        // ChatType|To|Msg
        int cmdSize = cmd.Length;
        int minCmdSize = Common.chtTypeSize + Common.idSize;
        int msgSize = cmd.Length - minCmdSize;
        if (cmdSize >= minCmdSize)
        {
            string strChatType = cmd.Substring(0, Common.chtTypeSize).Trim();
            string strToId = cmd.Substring(Common.chtTypeSize,Common.idSize).Trim();
            string msg = cmd.Substring(Common.chtTypeSize + Common.idSize,msgSize);
            if(string.Compare(strChatType,ChatType.Private.ToString(),true) == 0) // 私聊
            {                
                if (Singleton<MemberManager>.GetInstance().IsExist(strToId))
                {
                    Singleton<CommandSender>.GetInstance().SendChatCmd(ChatType.Private,clientId,strToId, msg);
                    Debug.Log($"{clientId}悄悄对{strToId}说{msg}!");
                }
            }
            else if(string.Compare(strChatType, ChatType.Public.ToString(), true) == 0) // 群聊
            {               
                if (Singleton<RoomManager>.GetInstance().IsExist(strToId))
                {
                    Singleton<CommandSender>.GetInstance().SendChatCmd(ChatType.Public, clientId, strToId, msg);
                    Debug.Log($"{clientId}在{strToId}房间说{msg}!");
                }
            }
            else
                Debug.Log("Chat type not available……");
        }
        else
            Debug.Log("Chat command not available……");       
    }   
}
